<?php

/**
 * 内容控制器
 */
class ContentController extends \Our\Controller_AbstractAdmin
{
    /**
     * 列表页
     */
    public function indexAction()
    {
        $page = $this->_getParam('page', 1);
        $count = $this->_getParam('count', 12);

        $where = $this->_getWhere();
        if (empty($where['`status` = ?'])) {
            $where['status'] = 1;
        }

        $ContentModel = \Content\ContentModel::getInstance();
        list($result, $paginator) = $ContentModel->contentList([
            'page' => $page,
            'count' => $count,
            'where' => $where,
            'baseUrl' => $this->_getBaseUrl()
        ]);

        $ContentDAOModel = \Content\ContentDAOModel::getInstance();

        $this->_assign('statusConfig', $ContentDAOModel->statusConfig);
        $this->getView()->assign('paginator', $paginator);
        $this->getView()->assign('listdata', $result);
    }

    /**
     * 添加内容
     */
    public function addAction()
    {
        $form = new \Content\ContentAddFormModel();
        if ($this->_isPost()) {
            $form->setData($this->_getPost());
        }

        if ($this->_isPost() && $form->validate()) {
            $params = $form->getFieldValue();
            $ContentModel = \Content\ContentModel::getInstance();
            $ContentModel->add($params);
            return $this->redirect("/admin/content/index/");
        }

        $CategoryDAOModel = \Category\CategoryDAOModel::getInstance();
        $categoryList = $CategoryDAOModel->fetchAll();

        $ContentDAOModel = \Content\ContentDAOModel::getInstance();

        $this->_assign('statusConfig', $ContentDAOModel->statusConfig);
        $this->_assign('categoryList', $categoryList);
        $this->_assign('form', $form);
        return true;
    }

    /**
     * 编辑内容
     */
    public function editAction()
    {
        $content = $this->_validateContent($this->_getQuery("content_id"));
        $form = new \Content\ContentAddFormModel();
        if ($this->_isGet()) {
            $content['add_time'] = date('Y-m-d H:i:s', $content['add_time']);
            $form->setData($content);
        }
        if ($this->_isPost()) {
            $form->setData($this->_getPost());
        }

        if ($this->_isPost() && $form->validate()) {
            try {
                $params = $form->getFieldValue();
                $params['content_id'] = $content['content_id'];

                $ContentModel = \Content\ContentModel::getInstance();
                $ContentModel->edit($params);
                $this->_assignSuccessMessage('保存成功');
            } catch (\Error\OurExceptionModel $e) {
                $this->_assignErrorMessage($e->getMessage(), $e->getCode());
            }
        }

        $CategoryDAOModel = \Category\CategoryDAOModel::getInstance();
        $categoryList = $CategoryDAOModel->fetchAll();

        $ContentDAOModel = \Content\ContentDAOModel::getInstance();

        $this->_assign('categoryList', $categoryList);
        $this->_assign('statusConfig', $ContentDAOModel->statusConfig);
        $this->_assign('content', $content);
        $this->_assign('form', $form);
        $this->_display('content/add.phtml');
    }

    /**
     * 删除
     */
    public function deleteAction()
    {
        $id = $this->_getQuery('id');
        $this->_validateContent($id);

        $ContentModel = \Content\ContentModel::getInstance();
        $result = $ContentModel->delete(array('content_id' => $id));

        $this->_echoSuccessMessage("删除内容成功", $result);
    }

    /**
     * 校验内容是否存在
     *
     * @param int $id
     * @return array
     */
    protected function _validateContent($id)
    {
        $ContentDAOModel = \Content\ContentDAOModel::getInstance();
        $model = $ContentDAOModel->find($id);
        if (!$model) {
            exit();
        }
        return $model;
    }
}
